package com.hxc.web.controller.basicdata;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hxc.enums.ReactionStateTypes;
import com.hxc.enums.ReactionTypes;
import com.hxc.interest.domain.TReactionInfo;
import com.hxc.interest.service.ITReactionInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hxc.common.annotation.Log;
import com.hxc.common.core.controller.BaseController;
import com.hxc.common.core.domain.AjaxResult;
import com.hxc.common.enums.BusinessType;
import com.hxc.basicdata.domain.TVillagerInfo;
import com.hxc.basicdata.service.ITVillagerInfoService;
import com.hxc.common.utils.poi.ExcelUtil;
import com.hxc.common.core.page.TableDataInfo;

/**
 * 村民信息Controller
 * 
 * @author hxc
 * @date 2021-08-11
 */
@Api(tags = "村民信息管理")
@RestController
@RequestMapping("/basicdata/villager")
public class TVillagerInfoController extends BaseController
{
    @Autowired
    private ITVillagerInfoService tVillagerInfoService;
    //事件信息Service
    @Autowired
    private ITReactionInfoService tReactionInfoService;

    /**
     * 查询村民信息列表
     */
    @PreAuthorize("@ss.hasPermi('basicdata:villager:list')")
    @GetMapping("/list")
    public TableDataInfo list(TVillagerInfo tVillagerInfo)
    {
        startPage();
        List<TVillagerInfo> list = tVillagerInfoService.selectTVillagerInfoList(tVillagerInfo);
        return getDataTable(list);
    }

    /**
     * 查询村民信息(不分页)
     */
    @PreAuthorize("@ss.hasPermi('basicdata:villager:list')")
    @GetMapping("/villagerList")
    public AjaxResult villagerList(TVillagerInfo tVillagerInfo)
    {
        List<TVillagerInfo> list = tVillagerInfoService.selectTVillagerInfoList(tVillagerInfo);
        return AjaxResult.success("success", list);
    }

    /**
     * 导出村民信息列表
     */
    @PreAuthorize("@ss.hasPermi('basicdata:villager:export')")
    @Log(title = "村民信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TVillagerInfo tVillagerInfo)
    {
        List<TVillagerInfo> list = tVillagerInfoService.selectTVillagerInfoList(tVillagerInfo);
        ExcelUtil<TVillagerInfo> util = new ExcelUtil<TVillagerInfo>(TVillagerInfo.class);
        return util.exportExcel(list, "村民信息数据");
    }

    /**
     * 获取村民信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('basicdata:villager:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tVillagerInfoService.selectTVillagerInfoById(id));
    }

    /**
     * 新增村民信息
     */
    @PreAuthorize("@ss.hasPermi('basicdata:villager:add')")
    @Log(title = "村民信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TVillagerInfo tVillagerInfo)
    {
        return toAjax(tVillagerInfoService.insertTVillagerInfo(tVillagerInfo));
    }

    /**
     * 修改村民信息
     */
    @PreAuthorize("@ss.hasPermi('basicdata:villager:edit')")
    @Log(title = "村民信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TVillagerInfo tVillagerInfo)
    {
        return toAjax(tVillagerInfoService.updateTVillagerInfo(tVillagerInfo));
    }

    /**
     * 删除村民信息
     */
    @PreAuthorize("@ss.hasPermi('basicdata:villager:remove')")
    @Log(title = "村民信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tVillagerInfoService.deleteTVillagerInfoByIds(ids));
    }

    @ApiOperation(value = "根据村民信息查询村民绑定信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "村民id", dataType = "Long"),
            @ApiImplicitParam(name = "villageName", value = "村民名称", dataType = "String"),
            @ApiImplicitParam(name = "groupName", value = "村民组别", dataType = "String")
    })
    @GetMapping("/queryVillagerByName")
    public AjaxResult queryVillagerByName(TVillagerInfo tVillagerInfo){
        TVillagerInfo villagerInfo = tVillagerInfoService.queryVillagerInfo(tVillagerInfo);
        return AjaxResult.success("success", villagerInfo);
    }

    @ApiOperation(value = "根据账户id获取信息及代办事项")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "账户主键", dataType = "Long"),
            @ApiImplicitParam(name = "communityId", value = "社区id", dataType = "Long"),
            @ApiImplicitParam(name = "communityGroupId", value = "社区组别id", dataType = "Long")
    })
    @GetMapping("/queryVillagerById")
    public AjaxResult queryVillagerById(TVillagerInfo tVillagerInfo){
        Map<String, Object> map = new HashMap<>();
        TVillagerInfo villagerInfo = tVillagerInfoService.queryVillagerInfo(tVillagerInfo);
        if(ObjectUtils.isEmpty(villagerInfo)){
            return AjaxResult.success("查询信息不存在");
        }
        map.put("villagerInfo", villagerInfo);
        TReactionInfo tReactionInfo = new TReactionInfo();
        tReactionInfo.setReactionType(ReactionTypes.ZJS.getCode());  //查询自己事
        tReactionInfo.setReactionStateEmus(ReactionStateTypes.YTJ.getCode()+","+ReactionStateTypes.CLZ.getCode());  //查询未处理事件
        tReactionInfo.setCommunityId(tVillagerInfo.getCommunityId());
        tReactionInfo.setCommunityGroupId(tVillagerInfo.getCommunityGroupId());
        List<TReactionInfo> reactionList = tReactionInfoService.queryHeadReactionList(tReactionInfo);
        map.put("reactionList", reactionList);
        return AjaxResult.success("success", map);
    }

}
